﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.VehicleManage
{
    public partial class Modify : Common.PageBase
    {
        T_VEHICLE_BLL bll = new T_VEHICLE_BLL();
        T_BUSINESSDIVISION_BLL bdBll = new T_BUSINESSDIVISION_BLL(); 
        private T_SUPPLIER_BLL supplierBll = new T_SUPPLIER_BLL();
        private T_MARKETDEPARTMENT_BLL marketDepartmentBll = new T_MARKETDEPARTMENT_BLL();
        private T_SERVICEPROVIDER_BLL serviceProviderBll = new T_SERVICEPROVIDER_BLL();
        private T_DEALER_BLL dealerBll = new T_DEALER_BLL();


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
            //PageHelper.SetPopCalender(this.Page, txtINNEROFFLINEDATE);
            PageHelper.SetPopCalender(this.Page, txtOFFLINEDATE);
            PageHelper.SetPopCalender(this.Page, txtMANUFACTUREDATE);
            PageHelper.SetPopCalender(this.Page, txtSTOCKDATE);
            PageHelper.SetPopCalender(this.Page, txtVERIFIERDATE);
            PageHelper.SetPopCalender(this.Page, txtFIRSTSALEDATE);
            //PageHelper.SetPopCalender(this.Page, txtTUTURNTIME);
            //PageHelper.SetPopCalender(this.Page, txtEXPORTDATE);
            //PageHelper.SetPopCalender(this.Page, txtONLINEDATE);
            //PageHelper.SetPopCalender(this.Page, txtTUBACKTIME);
            PageHelper.SetPopCalender(this.Page, txtCREATETIME);
            PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIME);
            //PageHelper.SetPopCalender(this.Page, txtCHANGEDATE);
			
        

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBUSINESSDIVISIONIDBinding();


                pageDataInit();
            }
        }

        #region 页面初始化数据绑定



        /// <summary>
        /// 供应商
        /// DDLs the SUPPLYCODE binding.
        /// </summary>
        private void ddlSUPPLYCODEBinding()
        {
            string businessDivisionCode = string.Empty;
            T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            if (bdEntity != null)
            {
                businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            }

            ddlSUPPLYCODE.DataSource = supplierBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and Status = {1} ", businessDivisionCode, ((int)Status.启用).ToString()));
            ddlSUPPLYCODE.DataTextField = "SupplierName";
            ddlSUPPLYCODE.DataValueField = "SupplierCode";
            ddlSUPPLYCODE.DataBind();
            ddlSUPPLYCODE.Items.Insert(0, new ListItem("", ""));
        }

       
        /// <summary>
        /// 经销商
        /// DDLs the DEALERCODE binding.
        /// </summary>
        private void ddlDEALERCODEBinding()
        {
            string businessDivisionCode = string.Empty;
            T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            if (bdEntity != null)
            {
                businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            }

            ddlDEALERCODE.DataSource = dealerBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and Status = {1}  and MarketDepartmentCode = '{2}' ", businessDivisionCode, ((int)Status.启用).ToString(), ddlMARKETDEPARTMENTCODE.SelectedValue.Trim())); ;
            ddlDEALERCODE.DataTextField = "DealerName";
            ddlDEALERCODE.DataValueField = "DealerCode";
            ddlDEALERCODE.DataBind();
            ddlDEALERCODE.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 市场部
        /// DDLs the MARKETDEPARTMENTCODE binding.
        /// </summary>
        private void ddlMARKETDEPARTMENTCODEBinding()
        {
            ddlMARKETDEPARTMENTCODE.Items.Clear();
            string businessDivisionCode = string.Empty;
            T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            if (bdEntity != null)
            {
                businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            }
            var list = marketDepartmentBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and Status = {1} ", businessDivisionCode, ((int)Status.启用).ToString()));
            ddlMARKETDEPARTMENTCODE.DataSource = list;
            ddlMARKETDEPARTMENTCODE.DataTextField = "MarketDepartmentName";
            ddlMARKETDEPARTMENTCODE.DataValueField = "MarketDepartmentCode";
            ddlMARKETDEPARTMENTCODE.DataBind();
            ddlMARKETDEPARTMENTCODE.Items.Insert(0, new ListItem("", ""));
        }





        /// <summary>
        ///	事业部编号 下拉列表数据绑定
        /// </summary>
        private void ddlBUSINESSDIVISIONIDBinding()
        {
            ddlBUSINESSDIVISIONID.DataSource = bdBll.GetCacheList(this.Page, String.Format(" and Status = {0} ", ((int)Status.启用).ToString()), "Status");
            ddlBUSINESSDIVISIONID.DataTextField = "BUSINESSDIVISIONNAME";
            ddlBUSINESSDIVISIONID.DataValueField = "BUSINESSDIVISIONID";
            ddlBUSINESSDIVISIONID.DataBind();

            if (!UserLoginInfo.IsAdministrator())
            {
                ddlBUSINESSDIVISIONID.SelectedValue = _userLoginInfo.UserEntity.BUSINESSDIVISIONID.ToString();
                ddlBUSINESSDIVISIONID.Enabled = false;
            }

        }

        /// <summary>
        /// 服务站
        /// DDLs the STATIONCODE binding.
        /// </summary>
        private void ddlSTATIONCODEBinding()
        {
            //string businessDivisionCode = string.Empty;
            //T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            //if (bdEntity != null)
            //{
            //    businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            //}

            //ddlSTATIONCODE.DataSource = serviceProviderBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and Status = {1} and MarketDepartmentCode = '{2}' ", businessDivisionCode, ((int)Status.启用).ToString(), ddlMARKETDEPARTMENTCODE.SelectedValue.Trim()));
            //ddlSTATIONCODE.DataTextField = "ServiceProviderName";
            //ddlSTATIONCODE.DataValueField = "ServiceProviderCode";
            //ddlSTATIONCODE.DataBind();
            //ddlSTATIONCODE.Items.Insert(0, new ListItem("", ""));
        }


        #endregion

        protected void ddlBUSINESSDIVISIONID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlMARKETDEPARTMENTCODEBinding();
            ddlMARKETDEPARTMENTCODE_SelectedIndexChanged(null, null);
            ddlSUPPLYCODEBinding();
        }

        protected void ddlMARKETDEPARTMENTCODE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDEALERCODEBinding();
            ddlSTATIONCODEBinding();
        }

        private void pageDataInit()
        {
            string _vehicleid = String.IsNullOrEmpty(Request.QueryString["VEHICLEID"]) ? "" : Request.QueryString["VEHICLEID"].Trim();

            if (!String.IsNullOrEmpty(_vehicleid))
            {
                T_VEHICLE_Entity entity = bll.GetEntity("VEHICLEID", _vehicleid);

                try
                {
                    ddlBUSINESSDIVISIONID.SelectedValue = entity.BUSINESSDIVISIONID.ToString();
                }
                catch (Exception ex)
                {
                    
                }
                ddlBUSINESSDIVISIONID_SelectedIndexChanged(null, null);
                try
                {
                    ddlMARKETDEPARTMENTCODE.SelectedValue = entity.MARKETDEPARTMENTCODE;
                }
                catch(Exception ex)
                {
                    
                }
                ddlMARKETDEPARTMENTCODE_SelectedIndexChanged(null, null);
                ddlDEALERCODEBinding();
                ddlSTATIONCODEBinding();
                ddlSUPPLYCODEBinding();
                try
                {
                    ddlDEALERCODE.SelectedValue = entity.DEALERCODE;
                }
                catch (Exception ex)
                {
                    
                }
                //ddlSTATIONCODE.SelectedValue = entity.STATIONCODE;
                try
                {
                    ddlSUPPLYCODE.SelectedValue = entity.SUPPLYCODE;
                }
                catch (Exception ex)
                {
                    
                }

                PageHelper.SetControlValue<T_VEHICLE_Entity>(this.Page, entity);
                //txtINNEROFFLINEDATE.Text = (entity.INNEROFFLINEDATE == null) ? "" : entity.INNEROFFLINEDATE.Value.ToString("yyyy-MM-dd");
                txtOFFLINEDATE.Text = (entity.OFFLINEDATE == null) ? "" : entity.OFFLINEDATE.Value.ToString("yyyy-MM-dd");
                txtMANUFACTUREDATE.Text = (entity.MANUFACTUREDATE == null) ? "" : entity.MANUFACTUREDATE.Value.ToString("yyyy-MM-dd");
                txtSTOCKDATE.Text = (entity.STOCKDATE == null) ? "" : entity.STOCKDATE.Value.ToString("yyyy-MM-dd");
                txtVERIFIERDATE.Text = (entity.VERIFIERDATE == null) ? "" : entity.VERIFIERDATE.Value.ToString("yyyy-MM-dd");
                txtFIRSTSALEDATE.Text = (entity.FIRSTSALEDATE == null) ? "" : entity.FIRSTSALEDATE.Value.ToString("yyyy-MM-dd");
                //txtTUTURNTIME.Text = (entity.TUTURNTIME == null) ? "" : entity.TUTURNTIME.Value.ToString("yyyy-MM-dd");
                //txtEXPORTDATE.Text = (entity.EXPORTDATE == null) ? "" : entity.EXPORTDATE.Value.ToString("yyyy-MM-dd");
                //txtONLINEDATE.Text = (entity.ONLINEDATE == null) ? "" : entity.ONLINEDATE.Value.ToString("yyyy-MM-dd");
                //txtTUBACKTIME.Text = (entity.TUBACKTIME == null) ? "" : entity.TUBACKTIME.Value.ToString("yyyy-MM-dd");
                txtCREATETIME.Text = (entity.CREATETIME == null) ? "" : entity.CREATETIME.Value.ToString("yyyy-MM-dd");
                txtLASTMODIFYTIME.Text = (entity.LASTMODIFYTIME == null) ? "" : entity.LASTMODIFYTIME.Value.ToString("yyyy-MM-dd");
                //txtCHANGEDATE.Text = (entity.CHANGEDATE == null) ? "" : entity.CHANGEDATE.Value.ToString("yyyy-MM-dd"); 

                lblTitle.Text = "车辆信息修改";
            }
            else
            {
                lblTitle.Text = "车辆信息添加";
                if (!UserLoginInfo.IsAdministrator())
                {
                    ddlBUSINESSDIVISIONID.SelectedValue = _userLoginInfo.UserEntity.BUSINESSDIVISIONID.ToString();
                    ddlBUSINESSDIVISIONID.Enabled = false;
                }
                ddlMARKETDEPARTMENTCODE_SelectedIndexChanged(null, null);
                ddlDEALERCODEBinding();
                ddlSTATIONCODEBinding();
                ddlSUPPLYCODEBinding();
            }

            txtCREATEPEOPLE.Enabled = false;
            txtCREATETIME.Enabled = false;
            txtLASTMODIFYPEOPLE.Enabled = false;
            txtLASTMODIFYTIME.Enabled = false;
            if (!UserLoginInfo.IsAdministrator())
            {
                ddlBUSINESSDIVISIONID.SelectedValue = _userLoginInfo.UserEntity.BUSINESSDIVISIONID.ToString();
                ddlBUSINESSDIVISIONID.Enabled = false;
            }

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _vehicleid = String.IsNullOrEmpty(Request.QueryString["VEHICLEID"]) ? "" : Request.QueryString["VEHICLEID"].Trim();


            T_VEHICLE_Entity entity = bll.GetEntity("VEHICLEID", _vehicleid);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_VEHICLE_Entity>(this.Page, ref entity);
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;
                T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
                if (bdEntity != null)
                {
                    entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
                }
                try
                {
                    entity.DEALERNAME = ddlDEALERCODE.SelectedItem.Text;
                }catch
                {
                }
                try
                {
                    entity.SUPPLYNAME = ddlSUPPLYCODE.SelectedItem.Text;
                }catch{}
                try
                {
                    entity.MARKETDEPARTMENTNAME = ddlMARKETDEPARTMENTCODE.SelectedItem.Text;
                }catch
                {
                }

                try
                {

                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "list.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_VEHICLE_Entity();

                PageHelper.GetControlValue<T_VEHICLE_Entity>(this.Page, ref entity);
                entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.CREATETIME = DateTime.Now;
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;
                T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
                if (bdEntity != null)
                {
                    entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
                }

                try
                {
                    entity.DEALERNAME = ddlDEALERCODE.SelectedItem.Text;
                }
                catch
                {
                }
                try
                {
                    entity.SUPPLYNAME = ddlSUPPLYCODE.SelectedItem.Text;
                }
                catch
                {
                }
                try
                {
                    entity.MARKETDEPARTMENTNAME = ddlMARKETDEPARTMENTCODE.SelectedItem.Text;
                }catch
                {
                }

                try
                {

                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "list.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失改！");
                    }

                }
                catch (Exception ex)
                {
                        PageHelper.ShowMessage(this.Page, "添加失改！" + ex.Message);
                }
            }
        }

        protected void btnAddSaveVehicle_Click(object sender, EventArgs e)
        {
            Response.Redirect(String.Format("~/VehicleManage/SaleModify.aspx?VehicleResourcesID={0}&CustomerCode={1}", PageHelper.GetQueryString(this.Page, "VehicleID"), PageHelper.GetQueryString(this.Page, "CustomerCode")));
        }
    }
}
